package com.example.myapplication.presenter.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.bean.CartBean;

import java.util.List;

public class CartListAdapter extends BaseAdapter {
    private List<CartBean> list = null;
    private CartListAdapter adapter = null;

    public CartListAdapter(List<CartBean> list) {
        this.list = list;
        adapter = this;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cartlist_adapter_layout, null);
            holder.selectPoint = convertView.findViewById(R.id.select_point);
            holder.icon = convertView.findViewById(R.id.icon);
            holder.name = convertView.findViewById(R.id.name);
            holder.price = convertView.findViewById(R.id.price);
            holder.sales = convertView.findViewById(R.id.sales);
            holder.cut = convertView.findViewById(R.id.cut);
            holder.add = convertView.findViewById(R.id.add);
            holder.number = convertView.findViewById(R.id.number);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.selectPoint.setImageResource(list.get(position).isSelect() ? R.drawable.select_red_point : R.drawable.select_white_point);
        holder.selectPoint.setTag(list.get(position));
        holder.selectPoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CartBean cb = ((CartBean) v.getTag());
//                Log.i("TAG", "onClick: " + cb.isSelect());
                cb.setSelect(!cb.isSelect());
                int id = R.drawable.select_white_point;
                if (cb.isSelect()) {
                    id = R.drawable.select_red_point;
                }
                ((ImageView) v).setImageResource(id);
            }
        });
        holder.cut.setTag(position);
        holder.cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CartBean cb = ((CartBean) list.get((Integer) v.getTag()));

                if (cb.getNumber() > 1) {
                    cb.setNumber(cb.getNumber() - 1);
                }
                Log.i("TAG", "onClick: " + cb.getNumber());
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
            }
        });
        holder.add.setTag(position);
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CartBean cb = ((CartBean) list.get((Integer) v.getTag()));
                cb.setNumber(cb.getNumber() + 1);
                Log.i("TAG", "onClick: " + cb.getNumber());
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
            }
        });
        holder.number.setTag(position);
        holder.number.setText("" + ((CartBean) list.get(position)).getNumber());
        return convertView;
    }

    private class ViewHolder {
        private ImageView selectPoint;
        private ImageView icon;
        private TextView name;
        private TextView price;
        private TextView sales;
        private TextView cut;
        private TextView add;
        private TextView number;
    }
}
